package com.example.administrator.ssc;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Response;

import static android.R.attr.data;

public class MainActivity extends AppCompatActivity {

    RelativeLayout rl_wan, rl_qian, rl_bai, rl_shi, rl_ge, rl_main;
    LinearLayout ll_item;
    TextView tv_wan, tv_qian, tv_bai, tv_shi, tv_ge, tv_percent, tv_count, tv_qishu, tv_last_result;
    TextView tv_wan_sha, tv_qian_sha, tv_bai_sha, tv_shi_sha, tv_ge_sha;
    List<Map<String, String>> listMap = new ArrayList<Map<String,String>>();
    List<String[]> listUrl = new ArrayList<String[]>();
    Button btn_refresh, btn_minus, btn_plus;
    private int qishu = 50;
    private CharSequence[] items = new CharSequence[]{"20", "30", "50", "100"};
    private int checkedItem = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FrameLayout parent = (FrameLayout) getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        rl_main = (RelativeLayout) parent.getChildAt(0);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        rl_wan = (RelativeLayout) findViewById(R.id.ll_wan);
        rl_qian = (RelativeLayout) findViewById(R.id.ll_qian);
        rl_bai = (RelativeLayout) findViewById(R.id.ll_bai);
        rl_shi = (RelativeLayout) findViewById(R.id.ll_shi);
        rl_ge = (RelativeLayout) findViewById(R.id.ll_ge);
        tv_qishu = (TextView) findViewById(R.id.tv_qishu);
        tv_last_result = (TextView) findViewById(R.id.tv_last_result);

        tv_wan = (TextView) rl_wan.findViewById(R.id.tv_title);
        tv_qian = (TextView) rl_qian.findViewById(R.id.tv_title);
        tv_bai = (TextView) rl_bai.findViewById(R.id.tv_title);
        tv_shi = (TextView) rl_shi.findViewById(R.id.tv_title);
        tv_ge = (TextView) rl_ge.findViewById(R.id.tv_title);
        tv_percent = (TextView) findViewById(R.id.tv_percent);
        tv_count = (TextView) findViewById(R.id.tv_count);
        tv_wan_sha = (TextView) rl_wan.findViewById(R.id.tv_sha);
        tv_qian_sha = (TextView) rl_qian.findViewById(R.id.tv_sha);
        tv_bai_sha = (TextView) rl_bai.findViewById(R.id.tv_sha);
        tv_shi_sha = (TextView) rl_shi.findViewById(R.id.tv_sha);
        tv_ge_sha = (TextView) rl_ge.findViewById(R.id.tv_sha);

        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String percentStr = tv_percent.getText().toString();
                int percent = Integer.parseInt(percentStr.replaceAll("%", ""));
                int resultPercent = percent - 1 < 0 ? 0 : percent - 1;
                tv_percent.setText(zhunQueLv = resultPercent + "%");
            }
        });
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String percentStr = tv_percent.getText().toString();
                int percent = Integer.parseInt(percentStr.replaceAll("%", ""));
                int resultPercent = percent + 1 > 100 ? 100 : percent + 1;
                tv_percent.setText(zhunQueLv = resultPercent + "%");
            }
        });
        btn_refresh = (Button) findViewById(R.id.btn_refresh);
        btn_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshData();
            }
        });

        tv_qishu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("选择期数")
                        .setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                qishu = Integer.parseInt(items[which].toString());
                                checkedItem = which;
                                tv_qishu.setText("当前期数：" + qishu);
                                dialog.dismiss();
                                refreshRequestList();
                                refreshData();
                            }
                        }).create();
                ad.show();
            }
        });

        tv_wan.setText("万位杀号");
        tv_qian.setText("千位杀号");
        tv_bai.setText("百位杀号");
        tv_shi.setText("十位杀号");
        tv_ge.setText("个位杀号");

        refreshRequestList();

        OkGo.init(getApplication());
//        OkGo.getInstance().debug("时时彩");
        OkGo.getInstance()
                .setRetryCount(3)
                .setConnectTimeout(5000)
                .setReadTimeOut(5000)
                .setWriteTimeOut(5000);

        refreshData();
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    Log.i("后台", appProcess.processName);
                    return true;
                }else{
                    Log.i("前台", appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }

    public void refreshRequestList(){
        listUrl.clear();
        listUrl.add(new String[]{
                "http://cp.360.cn/shdd/shax?LotID=255401&ItemID=1001&TopCount=" + qishu,
                "http://zst.cjcp.com.cn/shdd/chongqingssc-ww-" + qishu + ".html",
                "http://zx.caipiao.163.com/shahao/cqssc/wanwei_" + qishu + ".html",
                "http://www.woying.com/Lotterys/Gaopin/gpssc_killnum?type=1&count=" + qishu
        });
        listUrl.add(new String[]{
                "http://cp.360.cn/shdd/shax?LotID=255401&ItemID=1002&TopCount=" + qishu,
                "http://zst.cjcp.com.cn/shdd/chongqingssc-qw-" + qishu + ".html",
                "http://zx.caipiao.163.com/shahao/cqssc/qianwei_" + qishu + ".html",
                "http://www.woying.com/Lotterys/Gaopin/gpssc_killnum?type=2&count=" + qishu
        });
        listUrl.add(new String[]{
                "http://cp.360.cn/shdd/shax?LotID=255401&ItemID=1003&TopCount=" + qishu,
                "http://zst.cjcp.com.cn/shdd/chongqingssc-bw-" + qishu + ".html",
                "http://zx.caipiao.163.com/shahao/cqssc/baiwei_" + qishu + ".html",
                "http://www.woying.com/Lotterys/Gaopin/gpssc_killnum?type=3&count=" + qishu
        });
        listUrl.add(new String[]{
                "http://cp.360.cn/shdd/shax?LotID=255401&ItemID=1004&TopCount=" + qishu,
                "http://zst.cjcp.com.cn/shdd/chongqingssc-sw-" + qishu + ".html",
                "http://zx.caipiao.163.com/shahao/cqssc/shiwei_" + qishu + ".html",
                "http://www.woying.com/Lotterys/Gaopin/gpssc_killnum?type=4&count=" + qishu
        });
        listUrl.add(new String[]{
                "http://cp.360.cn/shdd/shax?LotID=255401&ItemID=1005&TopCount=" + qishu,
                "http://zst.cjcp.com.cn/shdd/chongqingssc-gw-" + qishu + ".html",
                "http://zx.caipiao.163.com/shahao/cqssc/gewei_" + qishu + ".html",
                "http://www.woying.com/Lotterys/Gaopin/gpssc_killnum?type=5&count=" + qishu
        });
    }

    int count = 60;
    int returnFourCount = 0;
    Handler handler = new Handler();
    Runnable callback;

    private void countDownTime() {

        count = 60;
        tv_count.setText(count + "秒后刷新");
        if(callback != null){
            handler.removeCallbacks(callback);
        }
        handler.postDelayed(callback = new Runnable() {
            @Override
            public void run() {
                count--;
                if(count <= 0){
                    if(count == 0){
                        refreshData();
                    }
                }else{
                    tv_count.setText(count + "秒后刷新");
                }
                handler.postDelayed(this, 1000);
            }
        }, 1000);

    }

    public void refreshData(){

        tv_count.setText("正在刷新...");

        returnFourCount = 0;
        wanMap.clear();
        qianMap.clear();
        baiMap.clear();
        shiMap.clear();
        geMap.clear();

//        tv_wan_sha.setText("准确率杀号：");
//        tv_qian_sha.setText("准确率杀号：");
//        tv_bai_sha.setText("准确率杀号：");
//        tv_shi_sha.setText("准确率杀号：");
//        tv_ge_sha.setText("准确率杀号：");

        OkGo.getInstance().cancelAll();

        for (int i = 0; i < ll_item.getChildCount(); i++) {
            RelativeLayout rl_each = (RelativeLayout) ll_item.getChildAt(i);
            LinearLayout ll_each = (LinearLayout) rl_each.getChildAt(0);
            for (int j = 1; j <= 4; j++) {
                ProgressBar pb = (ProgressBar) ((LinearLayout)ll_each.getChildAt(j)).getChildAt(2);
                pb.setVisibility(View.VISIBLE);
            }
        }

        for (int i = 0; i < listUrl.size(); i++) {
            String[] urls = listUrl.get(i);
            for (int j = 0; j <urls.length; j++) {
                String url = urls[j];
                TextView tv_type = null;
                if(j == 0){
                    tv_type = (TextView) ll_item.getChildAt(i).findViewById(R.id.tv_ssc_360);
                }else if(j == 1){
                    tv_type = (TextView) ll_item.getChildAt(i).findViewById(R.id.tv_ssc_cq);
                }else if(j == 2){
                    tv_type = (TextView) ll_item.getChildAt(i).findViewById(R.id.tv_ssc_wangyi);
                }else if(j == 3){
                    tv_type = (TextView) ll_item.getChildAt(i).findViewById(R.id.tv_ssc_biying);
                }
                tv_type.setText("");
                OkGo.get(url).tag(this).execute(new MyCallBack(i, j) {

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        e.printStackTrace();
//                        for (int i = 0; i < listUrl.size(); i++) {/
//                            setValue(i, webType, "");
//                        }
                        setValue(type, webType, "");
                        countDownTime();
                        if(!isBackground(MainActivity.this)){
                            Toast.makeText(MainActivity.this, "此次网站访问失败，请等待下次数据", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public String convertSuccess(Response response) throws Exception {
                        if(webType == 0 || webType == 1){
                            byte[] bytes = response.body().bytes();
                            response.close();
                            return new String(bytes, "GBK");
                        }
                        return super.convertSuccess(response);
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("请求", type + "," + webType);
                        returnFourCount++;
                        getData(type, webType, s);
                        if(returnFourCount == 20){
                            Toast.makeText(MainActivity.this, "最新" + qishu + "期杀号已全部刷新", Toast.LENGTH_LONG).show();
                            String wanText = getZhunText(wanMap);
                            String qianText = getZhunText(qianMap);
                            String baiText = getZhunText(baiMap);
                            String shiText = getZhunText(shiMap);
                            String geText = getZhunText(geMap);
                            tv_wan_sha.setText("准确率杀号：\n" + wanText);
                            tv_qian_sha.setText("准确率杀号：\n" + qianText);
                            tv_bai_sha.setText("准确率杀号：\n" + baiText);
                            tv_shi_sha.setText("准确率杀号：\n" + shiText);
                            tv_ge_sha.setText("准确率杀号：\n" + geText);
                            setBarView((LinearLayout) (ll_item.getChildAt(0).findViewById(R.id.ll_bar_view)), wanText);
                            setBarView((LinearLayout) (ll_item.getChildAt(1).findViewById(R.id.ll_bar_view)), qianText);
                            setBarView((LinearLayout) (ll_item.getChildAt(2).findViewById(R.id.ll_bar_view)), baiText);
                            setBarView((LinearLayout) (ll_item.getChildAt(3).findViewById(R.id.ll_bar_view)), shiText);
                            setBarView((LinearLayout) (ll_item.getChildAt(4).findViewById(R.id.ll_bar_view)), geText);
                            countDownTime();
                        }
                    }
                });
            }
        }

    }

//    private String getZhunText(Map<Integer, Set<String>> map){
//        String text = "";
//        List<Integer> sortList = sortByValue(map.keySet());
//        for (int k = 0; k < sortList.size(); k++) {
//            int key = sortList.get(k);
//            Set<String> set = map.get(key);
//            Object[] numbers = set.toArray();
//            String numStr = "";
//            for (int i1 = 0; i1 < numbers.length; i1++) {
//                numStr += numbers[i1] + ",";
//            }
//            if(!"".equals(numStr)){
//                numStr = numStr.substring(0, numStr.length() - 1);
//            }
//            text += key + "% ("+ numStr +")";
//            if(k != sortList.size() - 1){
//                text += "\n";
//            }
//
//        }
//        return text;
//    }

    private String getZhunText(Map<Integer, String> map){
        String text = "";
        List<Integer> sortList = sortByValue(map.keySet());
        for (int k = 0; k < sortList.size(); k++) {
            int key = sortList.get(k);
            String numStr = map.get(key);
//            if(!"".equals(numStr)){
//                numStr = numStr.substring(0, numStr.length() - 1);
//            }
            text += key + "% ("+ numStr +")";
            if(k != sortList.size() - 1){
                text += "\n";
            }

        }
        return text;
    }

    private int getNumberCount(int number, String data){
        String tempData = data.replaceAll("\n", "");
        String[] strs = tempData.split("\\)");

        String compareStr = "";
        String percentStr = tv_percent.getText().toString();
        int percent = Integer.parseInt(percentStr.replaceAll("%", ""));
        for (int i = 0; i < strs.length; i++) {

            String[] split = strs[i].split("% \\(");

            int comparePercent = Integer.parseInt(split[0]);
            String numberStr = split[1];

            if(comparePercent <= percent){
                compareStr += numberStr;
            }

//            String tempStrs[] = strs[i].split("% \\(", -1);
//            int itemPercent = 100;
//            try {
//                itemPercent = Integer.parseInt(tempStrs[0]);
//            } catch (NumberFormatException e) {
//                e.printStackTrace();
//            }
//            String numberStr = tempStrs[1];
//
//            compareStr += numberStr;

//            comparPercent = percent;
//            if(i == 0){
//                comparPercent = Integer.parseInt(strs[i]);
//            }
//            try {
//                comparPercent = Integer.parseInt(strs[i].replace("%", ""));
//            } catch (NumberFormatException e) {
//                e.printStackTrace();
//            }
//            if(i % 2 == 1){
//                compareStr += strs[i].split("\\)")[0];
//            }
        }
        Log.e("compareStr", "***" + compareStr);
        return compareStr.length() - compareStr.replaceAll(number + "", "").length();
    }

    private void setBarView(LinearLayout ll_bar, String data){

        Log.e("次数", "===========开始==============");
        int maxCount = 0;
        for (int i = 0; i < 10; i++) {
            int count = getNumberCount(i, data);
            Log.e("次数", i + "出现" + count + "次");
            maxCount = maxCount < count ? count : maxCount;
        }

        LinearLayout child = (LinearLayout) ll_bar.getChildAt(0);

        for (int i = 1; i < child.getChildCount() - 1; i++) {
            LinearLayout ll_child = (LinearLayout) child.getChildAt(i);
            RelativeLayout rl_child = (RelativeLayout) ll_child.getChildAt(0);
            TextView textView = (TextView) rl_child.getChildAt(0);
            int count = getNumberCount(i - 1, data);
            textView.setText(count + "");
            float heightPercent = (count + 0f) / maxCount;
            int height = (int) (heightPercent * getResources().getDisplayMetrics().density * 80);
            ViewGroup.LayoutParams lp = textView.getLayoutParams();
            if(lp.height != height){
                lp.height = height;
                textView.requestLayout();
            }
        }
    }

//    public static Set<Object> sortByValue(Set<Object> set){
//        List<Object> setList= new ArrayList<Object>(set);
//        Collections.sort(setList, new Comparator<Object>() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                // TODO Auto-generated method stub
//                return o1.toString().compareTo(o2.toString());
//            }
//
//        });
//        set = new LinkedHashSet<Object>(setList);//这里注意使用LinkedHashSet
//        return set;
//    }

    public static List<Integer> sortByValue(Set<Integer> set){
        List<Integer> setList= new ArrayList<Integer>(set);
        Collections.sort(setList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                // TODO Auto-generated method stub
                return o2 - o1;
            }

        });
        return setList;//这里注意使用LinkedHashSet
    }

    private String zhunQueLv = "100%";
//    private Map<Integer, Set<String>> wanMap = new LinkedHashMap<>();
//    private Map<Integer, Set<String>> qianMap = new LinkedHashMap<>();
//    private Map<Integer, Set<String>> baiMap = new LinkedHashMap<>();
//    private Map<Integer, Set<String>> shiMap = new LinkedHashMap<>();
//    private Map<Integer, Set<String>> geMap = new LinkedHashMap<>();
    private Map<Integer,String> wanMap = new LinkedHashMap<>();
    private Map<Integer, String> qianMap = new LinkedHashMap<>();
    private Map<Integer, String> baiMap = new LinkedHashMap<>();
    private Map<Integer, String> shiMap = new LinkedHashMap<>();
    private Map<Integer, String> geMap = new LinkedHashMap<>();

//    private void countMaxData(int type, int zhun, String num){
//        Log.e("数据~~", type + "~~");
//        if(type == 0){
//            Set<String> set = wanMap.get(zhun);
//            if(set == null){
//                set = new LinkedHashSet<>();
//                wanMap.put(zhun, set);
//            }
//            if(!set.contains(zhun)){
//                set.add(num);
//            }
//        }else if(type == 1){
//            Set<String> set = qianMap.get(zhun);
//            if(set == null){
//                set = new LinkedHashSet<>();
//                qianMap.put(zhun, set);
//            }
//            if(!set.contains(zhun)){
//                set.add(num);
//            }
//        }else if(type == 2){
//            Set<String> set = baiMap.get(zhun);
//            if(set == null){
//                set = new LinkedHashSet<>();
//                baiMap.put(zhun, set);
//            }
//            if(!set.contains(zhun)){
//                set.add(num);
//            }
//        }else if(type == 3){
//            Set<String> set = shiMap.get(zhun);
//            if(set == null){
//                set = new LinkedHashSet<>();
//                shiMap.put(zhun, set);
//            }
//            if(!set.contains(zhun)){
//                set.add(num);
//            }
//        }else if(type == 4){
//            Set<String> set = geMap.get(zhun);
//            if(set == null){
//                set = new LinkedHashSet<>();
//                geMap.put(zhun, set);
//            }
//            if(!set.contains(zhun)){
//                set.add(num);
//            }
//        }
//    }

    private void countMaxData(int type, int zhun, String num){
        Log.e("数据~~", type + "~~");
        if(type == 0){
            String data = wanMap.get(zhun);
            if(data == null){
                data = "";
            }
            data += num;
            wanMap.put(zhun, data);
        }else if(type == 1){
            String data = qianMap.get(zhun);
            if(data == null){
                data = "";
            }
            data += num;
            qianMap.put(zhun, data);
        }else if(type == 2){
            String data = baiMap.get(zhun);
            if(data == null){
                data = "";
            }
            data += num;
            baiMap.put(zhun, data);
        }else if(type == 3){
            String data = shiMap.get(zhun);
            if(data == null){
                data = "";
            }
            data += num;
            shiMap.put(zhun, data);
        }else if(type == 4){
            String data = geMap.get(zhun);
            if(data == null){
                data = "";
            }
            data += num;
            geMap.put(zhun, data);
        }
    }

    private String getData(int type, int webType, String webXmlData){

        Log.e("returnFourCount", "returnFourCount---" + returnFourCount);

        if(webType == 0){//360
            try {
                String str = webXmlData.substring(webXmlData.indexOf("当前期杀号"));
                String[] strs = str.split("--");
                String strZhun = webXmlData.substring(webXmlData.indexOf("准确率"));
                String[] strZhuns = strZhun.split("<strong class=\"red\">");
                String data = "";
                for (int i = 0; i < strs.length; i++) {

                    //此段获取数字值
                    String str1 = strs[i];
                    String frontStr = "width='35'>";
                    String str2 = str1.substring(str1.indexOf(frontStr) + frontStr.length());
                    String number = str2.substring(0, str2.indexOf("</b>")).replaceAll("\n", "").trim();;

                    //此段获取数字值对应的准确率
                    str2 = strZhuns[i +1];
                    String numberZhun = str2.substring(0, str2.indexOf("</strong>"));

                    //记录每个位置最高准确率数字
                    try {
                        int zhun = Integer.parseInt(numberZhun.replaceAll("%", ""));
                        countMaxData(type, zhun, number);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }


                    //如果准确率相符加入最后显示结果中
                    if(numberZhun.equals(zhunQueLv) && data.indexOf(number) == -1){
                        data += number + " ";
                    }

                    if(i == 9){
                        setValue(type, webType, data);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                setValue(type, webType, "数据异常");
            }
        }else if(webType == 1){//重庆
            try {
                String str = webXmlData.substring(webXmlData.indexOf("当前期杀号"));
                String[] strs = str.split("<td class=\"winning_red\">");
                String strZhun = webXmlData.substring(webXmlData.indexOf("准确率"));
                String[] strZhuns = strZhun.split("<span class=\"font_red_blod\">");
                String data = "";
                for (int i = 1; i < strs.length; i++) {

                    //此段获取数字值
                    String str1 = strs[i];
                    String number = str1.substring(0, 1).replaceAll("\n", "").trim();;

                    //此段获取数字值对应的准确率
                    String str2 = strZhuns[i];
                    String numberZhun = str2.substring(0, str2.indexOf("</td>"));

                    //记录每个位置最高准确率数字
                    try {
                        int zhun = Integer.parseInt(numberZhun.replaceAll("%", ""));
                        countMaxData(type, zhun, number);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    //如果准确率相符加入最后显示结果中
                    if(numberZhun.equals(zhunQueLv) && data.indexOf(number) == -1){
                        data += number + " ";
                    }

                    Log.e("datadata", data);

                    if(i == 10){
                        setValue(type, webType, data);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                setValue(type, webType, "数据异常");
            }
        }else if(webType == 2){//网易

            try {
                String str = webXmlData.substring(webXmlData.lastIndexOf("当前杀号"));
                String[] strs = str.split("<span class=\"select\">");
                String strZhun = webXmlData.substring(webXmlData.indexOf("准确率"));
                String[] strZhuns = strZhun.split("</td>");
                String data = "";
                for (int i = 1; i < strs.length; i++) {

                    //此段获取数字值
                    String str1 = strs[i];
                    String number = str1.substring(0, 1).replaceAll("\n", "").trim();

                    //此段获取数字值对应的准确率
                    String str2 = strZhuns[i];
                    int index = str2.indexOf(">");
                    String numberZhun = str2.substring(index + 1);

                    if("全对".equals(numberZhun))
                        numberZhun = "100%";

                    //记录每个位置最高准确率数字
                    try {
                        int zhun = Integer.parseInt(numberZhun.replaceAll("%", ""));
                        countMaxData(type, zhun, number);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    //如果准确率相符加入最后显示结果中
                    if(numberZhun.equals(zhunQueLv) && data.indexOf(number) == -1){
                        data += number + " ";
                    }

                    if(i == 10){
                        setValue(type, webType, data);
                        break;
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
                setValue(type, webType, "数据异常");
            }


        }else if(webType == 3){//必赢

            try {
                String[] datas = webXmlData.split("当前期杀号");
                String lastResultStr = datas[0];
                lastResultStr = lastResultStr.substring(0, lastResultStr.lastIndexOf("</tr>"));
                lastResultStr = lastResultStr.substring(lastResultStr.lastIndexOf("<tr"));
                String[] split = lastResultStr.split("</td>");
                String lastResult = split[1].replaceAll("<td>", "").replaceAll("\n", "").trim();
                tv_last_result.setText("上期结果：" + lastResult);

                String str = datas[1];
                String[] strs = str.split("&nbsp;");
                String strZhun = webXmlData.substring(webXmlData.indexOf("准确率"));
                String[] strZhuns = strZhun.split("<td class=\"right_bottom\" colspan=\"2\">");

                String data = "";
                Log.e("strs", strs.length + "");
                for (int i = 0; i < strs.length; i++) {

                    //此段获取数字值
                    String str1 = strs[i];
                    String spliStr1 = "<td>";
                    String spliStr2 = "</td>";
                    String number = str1.substring(str1.indexOf(spliStr1) + spliStr1.length(), str1.lastIndexOf(spliStr2) - spliStr2.length() + 1);
                    number = number.replaceAll("\n", "").trim();

                    //此段获取数字值对应的准确率
                    String str2 = strZhuns[i + 1];
                    String splitStr = "%";
                    String numberZhun = str2.substring(str2.indexOf(splitStr) - 2, str2.indexOf(splitStr) + 1);

                    if("00%".equals(numberZhun)){
                        numberZhun = "100%";
                    }

                    //记录每个位置最高准确率数字
                    try {
                        int zhun = Integer.parseInt(numberZhun.replaceAll("%", ""));
                        countMaxData(type, zhun, number);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }


                    //如果准确率相符加入最后显示结果中
                    if(numberZhun.equals(zhunQueLv) && data.indexOf(number) == -1){
                        data += number + " ";
                    }

                    if(i == 7){
                        setValue(type, webType, data);
                        break;
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
                setValue(type, webType, "数据异常");
            }

        }

        return "--";
    }

    private void setValue(int position, int webType, String data){
        TextView tv_type = null;
        ProgressBar pb_type = null;
        if(webType == 0){
            tv_type = (TextView) ll_item.getChildAt(position).findViewById(R.id.tv_ssc_360);
            pb_type = (ProgressBar) ll_item.getChildAt(position).findViewById(R.id.pb_ssc_360);
        }else if(webType == 1){
            tv_type = (TextView) ll_item.getChildAt(position).findViewById(R.id.tv_ssc_cq);
            pb_type = (ProgressBar) ll_item.getChildAt(position).findViewById(R.id.pb_ssc_cq);
        }else if(webType == 2){
            tv_type = (TextView) ll_item.getChildAt(position).findViewById(R.id.tv_ssc_wangyi);
            pb_type = (ProgressBar) ll_item.getChildAt(position).findViewById(R.id.pb_ssc_wangyi);
        }else if(webType == 3){
            tv_type = (TextView) ll_item.getChildAt(position).findViewById(R.id.tv_ssc_biying);
            pb_type = (ProgressBar) ll_item.getChildAt(position).findViewById(R.id.pb_ssc_biying);
        }
        tv_type.setText(TextUtils.isEmpty(data) ? "--" : data);
        pb_type.setVisibility(View.INVISIBLE);
    }

    class MyCallBack extends  StringCallback{

        public int type;
        public int webType;

        MyCallBack(int type, int webType){
            this.type = type;
            this.webType = webType;
        }

        @Override
        public void onSuccess(String s, Call call, Response response) {

        }
    }

}
